package top.leyi.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.leyi.IService.IApproveService;
import top.leyi.IService.IAuthService;
import top.leyi.IService.IPushService;
import top.leyi.entity.Approve;
import top.leyi.entity.SQLUtil;
import top.leyi.entity.Talk.Talk;
import top.leyi.entity.user.User;
import top.leyi.exception.MutiSubmitException;
import top.leyi.mapper.ApproveMapper;
import top.leyi.mapper.UserMapper;

import java.util.List;

@Service
public class ApproveServiceImpl implements IApproveService {
@Autowired
    ApproveMapper approveMapper;
    @Autowired
    IAuthService authService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    IPushService pushService;

    @Override
    public void approveFail(Approve approve, String msg) throws Exception{
        approveMapper.updateFailState(approve.getId());


        Talk talk = new Talk();
        talk.setContent("抱歉，认证失败，原因:"+msg);
        talk.setUser_id(approve.getUser_id());
        pushService.addOnePush(talk, approve.getUser_id());
    }

    @Override
    @Transactional
    public void approveSuc(Approve approve) throws Exception{
        approveMapper.updateSucState(approve.getId());
        User user = new User();
        user.setId(approve.getUser_id());
        //修改人物,将人物id改为2，表示已经认证完成
        user.setRoleId(2);
        authService.changeUserRole(user);

        //发送通知给提交人员

        Talk talk = new Talk();
        talk.setContent("恭喜你，认证已成功");
        talk.setUser_id(approve.getUser_id());
        pushService.addOnePush(talk, approve.getUser_id());
    }

    @Override
    public List<Approve> queryUserApproveStatus(User user) {

        return approveMapper.queryPersonalApprove(user.getId());
    }

    @Override
    public void cancelApprove(Approve approve) {

        approveMapper.removeApprove(approve.getId());
    }

    @Override
    public void addApprove(Approve approve)throws Exception {
         int count = approveMapper.countApprove(approve);
         if(count > 0) {
             throw new MutiSubmitException();
         }
         approveMapper.addApprove(approve);
    }

    public List<Approve> queryAllApprove(int page, int state) {
        SQLUtil sqlUtil = new SQLUtil();
        sqlUtil.setLimit(10);
        sqlUtil.setOffset((page - 1) * 10);
        sqlUtil.setId(state);
        List<Approve> approves = approveMapper.queryAllApprove(sqlUtil);
        for(Approve a : approves) {
            a.setUser(userMapper.queryOneUser(a.getUser_id()));
        }
        return approves;
    }

    @Override
    public List<Approve> queryUndealApprove(int page) {

        return queryAllApprove(page, 0);
    }


    @Override
    public List<Approve> queryFailApprove(int page) {
        return queryAllApprove(page, 2);
    }

    @Override
    public List<Approve> querySucApprove(int page) {
        return queryAllApprove(page, 1);
    }
}
